package cbir;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;


public class Grafico extends Applet {

	private static final long serialVersionUID = 1L;

	private float[] x;
	private float[] y;
	
	@Override
	public void init() {
		this.x = new float[15];
		
		// Parsing
		File f=new File("/home/math/workspace/UploadImage/f.txt");
		FileInputStream fis;
		try {
			fis = new FileInputStream(f);
			InputStreamReader isr=new InputStreamReader(fis);
			BufferedReader br=new BufferedReader(isr);
			
			String linea=br.readLine();
			
			int k = 0;
			while(linea!=null) {
					x[k] = Float.parseFloat(linea);
//					System.out.println("x[" + k + "] = " + x[k]);
					linea=br.readLine();
					k++;
			}
		} 
		catch (FileNotFoundException e) {e.printStackTrace();} 
		catch (IOException e) {e.printStackTrace();}

		float[] x_new = new float[15];		

		for(int i = 0; i < x_new.length; i++){
			x_new[i] = -(float)Math.log(x[i]);
		}
		
		for(int i = 0; i < x_new.length; i++){
			x_new[i] = x_new[i] * (i+1.5f);		
		}

			
		System.out.println("\nDopo op");
		for(int i = 0; i < x_new.length; i++)
			System.out.println("x[" + i + "] = " + x_new[i]);
		
		// Clipping
		float max = -Float.MAX_VALUE, min = Float.MAX_VALUE;
		System.out.println("max = " +max + "\tmin = " +min);
		
		for(int i = 0; i < x_new.length; i++){
			if(x_new[i] >= max)
				max = x_new[i];
			if(x_new[i] <= min)
				min = x_new[i];
		}
		
		System.out.println("max = " +max + "\tmin = " +min);
		System.out.println();
		y = new float[15];
		for(int i = 0; i < y.length; i++){
			y[i] = 1 - ( x_new[i] - min ) / (max - min) ;
			System.out.println("y[" + i + "] = " + y[i]);
		}
		
		
		setBackground(Color.white);
		setSize(650, 650);
	}

	@Override
	public void paint(Graphics g) {
		g.setColor(Color.black);
		g.drawLine(40, 610, 640, 610);	// asse x
		g.drawLine(40, 610, 40, 10);		// asse y
		
		// L'area disegnabile è da 10 a 510, sia sulle x che sulle y
		
		for(int j = 0; j < x.length; j++){
			int xx = (int) (x[j] * 600) + 40;
			int yy = (int) (610 - (y[j] * 600) );
			
			g.setColor(Color.red);
			g.drawLine(xx, 610, xx, 620);
			g.drawLine(40, yy, 30, yy);
			
			g.setColor(Color.green);
			g.drawLine(xx, 610, xx, yy);
			g.drawLine(40, yy, xx, yy);
			
			g.setColor(Color.blue);
			g.drawOval(xx-2, yy-2, 4, 4);
			g.fillOval(xx-2, yy-2, 4, 4);
			
		}

		g.setColor(Color.black);

		for(int i = 640; i >= 40; i = i - 60)
			g.drawLine(i, 610, i, 600);
		
		for(int i = 610; i >= 10; i = i-60)
				g.drawLine(40, i, 50, i);
	}
}
